setwd("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE")
library(haven)
library(dplyr)
library(janitor)

Feb2020 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/2 Feb 2020/UCL_DEL_Sandbox_FR_Wave2_Data.sav")
Jul2020 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/3 July 2020/UCL_DEL_Sandbox_FR_Wave3_Data.sav")
May2021 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/4 May 2021/UCL_DEL_Sandbox_FR_Wave4_Data.sav")
Jul2021 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/5 July 2021/UCL_DEL_Sandbox_FR_Wave5_Data.sav")
Jan2022 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/6 Jan 2022/UCL_DEL_Sandbox_France_Jan2022_client.sav")
Jun2022 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/7 June 2022/UCL_DEL_Sandbox_France_June2022_client.sav")
May2023 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/May 2023/UCL_DEL_Sandbox_France_May2023_client.sav")

#change weight variable name
Feb2020 <- Feb2020 %>%
  rename(Weight =  WeightSandboxW2)
Jul2020 <- Jul2020 %>%
  rename(Weight = WeightSandboxW3)
May2021 <- May2021 %>%
  rename(Weight = WeightSandboxW4)
Jul2021 <- Jul2021 %>%
  rename(Weight = WeightSandboxW5)
Jan2022 <- Jan2022 %>%
  rename(Weight = weight)
Jun2022 <- Jun2022 %>%
  rename(Weight = weight)
May2023 <- May2023 %>%
  rename(Weight = WeightSandboxFR)

#Feb 2020: change education variable name and political ideology name
Feb2020 <- Feb2020 %>%
  rename(education = education_FR,
         pb4_politicalscale = pb4_leftrightscale,
         ppol_partyid = ppol_partyID)

#May 2021
table(May2021$sandbox17)
table(May2021$sandbox25)

May2021$sandbox17 <- as.numeric(May2021$sandbox17)
May2021$sandbox25 <- as.numeric(May2021$sandbox25)

May2021 <- May2021 %>%
  mutate(sandbox17 = dplyr::recode(sandbox17, '12'=11),
         sandbox25 = dplyr::recode(sandbox25, '12'=11))

#Jul2021
Jul2021 <- Jul2021 %>%
  rename(income = tincome)

#Jan2022
Jan2022$leftright <- as.numeric(Jan2022$leftright)

Jan2022 <- Jan2022 %>%
  mutate(leftright = dplyr::recode(leftright, '12'=11),
         aidpercept_1 = aidpercept_1 -1,
         aidpercept_2 = aidpercept_2 -1)

table(Jan2022$leftright)
table(Jan2022$aidpercept_1)
table(Jan2022$aidpercept_2)

#Jun2022
Jun2022 <- Jun2022 %>%
  mutate(afr_influ2 = afr_influ2 -1)
table(Jun2022$afr_influ2)

FR_comp <- compare_df_cols(Feb2020, Jul2020, May2021, Jul2021, Jan2022, Jun2022, May2023)

write_sav(Feb2020, "UCL_DEL_Sandbox_FR_Feb2020_Data.sav")
write_sav(Jul2020, "UCL_DEL_Sandbox_FR_Jul2020_Data.sav")
write_sav(May2021, "UCL_DEL_Sandbox_FR_May2021_Data.sav")
write_sav(Jul2021, "UCL_DEL_Sandbox_FR_Jul2021_Data.sav")
write_sav(Jan2022, "UCL_DEL_Sandbox_FR_Jan2022_Data.sav")
write_sav(Jun2022, "UCL_DEL_Sandbox_FR_Jun2022_Data.sav")
write_sav(May2023, "UCL_DEL_Sandbox_FR_May2023_Data.sav")

May2023 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/May 2023/UCL_DEL_Sandbox_FR_May2023_Data.sav")
May2023 <- May2023 %>%
  rename(pb4_politicalscale = pol_id_scale,
         ppol_partyid = partyid_long)
table(May2023$pb4_politicalscale)

table(May2023$ppol_partyid)
class(May2023$ppol_partyid)

May2023$ppol_partyid <- as.numeric(May2023$ppol_partyid)
May2023 <- May2023 %>%
  mutate(ppol_partyid = dplyr::recode(ppol_partyid,
                                      "15" = 12,
                                      "12" = 955,
                                      "13" = 966,
                                      "14" = 977,
                                      "98" = 998,
                                      "99" = 999))
                                  
May2023 <- May2023 %>%
  mutate(ppol_partyid = recode_factor(ppol_partyid, `12` = 15,
                `955` = 12,
                `966` = 13,
                `977` = 14,
                `998` = 98))

Feb2020 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/2 Feb 2020/UCL_DEL_Sandbox_FR_Feb2020_Data.sav")
Jul2020 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/3 July 2020/UCL_DEL_Sandbox_FR_Jul2020_Data.sav")
May2021 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/4 May 2021/UCL_DEL_Sandbox_FR_May2021_Data.sav")
Jul2021 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/5 July 2021/UCL_DEL_Sandbox_FR_Jul2021_Data.sav")
Jan2022 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/6 Jan 2022/UCL_DEL_Sandbox_FR_Jan2022_Data.sav")
Jun2022 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/7 June 2022/UCL_DEL_Sandbox_FR_Jun2022_Data.sav")

Feb2020$pb4_politicalscale <- as.numeric(Feb2020$pb4_politicalscale)
Jul2020$pb4_politicalscale <- as.numeric(Jul2020$pb4_politicalscale)
May2021$pb4_politicalscale <- as.numeric(May2021$pb4_politicalscale)
Jul2021$pb4_politicalscale <- as.numeric(Jul2021$pb4_politicalscale)
Jan2022$pb4_politicalscale <- as.numeric(Jan2022$pb4_politicalscale)
Jun2022$pb4_politicalscale <- as.numeric(Jun2022$pb4_politicalscale)

table(Feb2020$pb4_politicalscale)
table(Jul2020$pb4_politicalscale)
table(May2021$pb4_politicalscale)
table(Jul2021$pb4_politicalscale)
table(Jan2022$pb4_politicalscale)
table(Jun2022$pb4_politicalscale)

Feb2020 <- Feb2020 %>%
  mutate(pb4_politicalscale = dplyr::recode(pb4_politicalscale,
                                            "8" = 98,
                                            "9" = 99))
Jul2020 <- Jul2020 %>%
  mutate(pb4_politicalscale = dplyr::recode(pb4_politicalscale,
                                            "8" = 98,
                                            "9" = 99))
May2021 <- May2021 %>%
  mutate(pb4_politicalscale = dplyr::recode(pb4_politicalscale,
                                            "8" = 98,
                                            "9" = 99))
Jul2021 <- Jul2021 %>%
  mutate(pb4_politicalscale = dplyr::recode(pb4_politicalscale,
                                            "8" = 98,
                                            "9" = 99))
Jan2022 <- Jan2022 %>%
  mutate(pb4_politicalscale = dplyr::recode(pb4_politicalscale,
                                            "8" = 98,
                                            "9" = 99))
Jun2022 <- Jun2022 %>%
  mutate(pb4_politicalscale = dplyr::recode(pb4_politicalscale,
                                            "8" = 98,
                                            "9" = 99))

write_sav(Feb2020, "UCL_DEL_Sandbox_FR_Feb2020_Data.sav")
write_sav(Jul2020, "UCL_DEL_Sandbox_FR_Jul2020_Data.sav")
write_sav(May2021, "UCL_DEL_Sandbox_FR_May2021_Data.sav")
write_sav(Jul2021, "UCL_DEL_Sandbox_FR_Jul2021_Data.sav")
write_sav(Jan2022, "UCL_DEL_Sandbox_FR_Jan2022_Data.sav")
write_sav(Jun2022, "UCL_DEL_Sandbox_FR_Jun2022_Data.sav")
write_sav(May2023, "UCL_DEL_Sandbox_FR_May2023_Data.sav")

May2023 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/FRANCE/UCL_DEL_Sandbox_FR_May2023_Data.sav")

May2023$ppol_partyid <- as.numeric(May2023$ppol_partyid)
May2023 <- May2023 %>%
  mutate(ppol_partyid = dplyr::recode(ppol_partyid,
                                      "12" = 8,
                                      "8" = 9,
                                      "9" = 10,
                                      "10" = 12))
write_sav(May2023, "UCL_DEL_Sandbox_FR_May2023_Data.sav")
table(May2023$ppol_partyid)
